package com.lianxi.nmms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lianxi.nmms.constant.DictConstant;
import com.lianxi.nmms.constant.StatusConstant;
import com.lianxi.nmms.dao.DeptMapper;
import com.lianxi.nmms.dao.StaffMapper;
import com.lianxi.nmms.entity.*;
import com.lianxi.nmms.exception.*;
import com.lianxi.nmms.service.StaffService;
import com.lianxi.nmms.util.MD5Util;
import com.lianxi.nmms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 翔哥
 * 2025年09月28日
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class StaffServiceImpl implements StaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Staff> findPage(Staff staffAll,String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.STAFF_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.STAFF_DEFAULT_PAGE_SIZE;
        }

        PageHelper.startPage(Integer.valueOf(pageNo), Integer.valueOf(pageSize));

        List<Staff> staffs = staffMapper.selectByStaff(staffAll);

        PageInfo<Staff> staffPage = new PageInfo<>(staffs);
        return staffPage;
    }


    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<Staff> findAll() {
        StaffExample staffExample =new StaffExample();
        return staffMapper.selectByExample(staffExample);
    }

    @Override
    public Staff login(String loginName, String password, String role, String code, String image) throws RequestParameterErrorException, CodeErrorException, StaffNotExistException {
        if(ParameterUtil.isNull(loginName)||ParameterUtil.isNull(password)||ParameterUtil.isNull(role)||ParameterUtil.isNull(code)||ParameterUtil.isNull(image)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        if(!code.equals(image)){
            throw new CodeErrorException("验证码不正确");
        }
        StaffExample example =new StaffExample();
        example.or()
                .andLoginNameEqualTo(loginName)
                .andPasswordEqualTo(MD5Util.md5(password))
                .andRoleEqualTo(role)
                .andStatusEqualTo(StatusConstant.STAFF_STATUS_ENABLED);
        List<Staff> staffs =staffMapper.selectByExample(example);

        if(staffs.isEmpty()){
            throw new StaffNotExistException("账号或密码错误");
        }
        return staffs.get(0);


    }
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Staff findStaffById(String id) throws RequestParameterErrorException {
        if(ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        Staff staff = staffMapper.selectByPrimaryKey(Integer.valueOf(id) );
        return staff;
    }

    @Override
    public void addStaff(String name, String loginName, String password, String phone, String email, String role, String deptId, HttpSession session) throws RequestParameterErrorException, loginErrorException {
        if(ParameterUtil.isNull(name)||ParameterUtil.isNull(loginName)||ParameterUtil.isNull(password)||ParameterUtil.isNull(phone)||ParameterUtil.isNull(email)||ParameterUtil.isNull(role)||ParameterUtil.isNull(deptId)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        Staff staff2 = (Staff) session.getAttribute("staff");
        if(staff2 ==null){
            throw new loginErrorException("还没有用户登录");
        }

        if(!ParameterUtil.setPassword(password)){
            throw new RequestParameterErrorException("密码有误:格式为数字或字母");
        }
        if (!ParameterUtil.setPhone(phone)){
            throw new RequestParameterErrorException("电话号码有误:格式为1开头，总共11位数字");
        }
        if (!ParameterUtil.setEmail(email)){
            throw new RequestParameterErrorException("邮箱有误:格式为XXX@XXX.XXX，X只能是数字或字母");
        }
        password = MD5Util.md5(password);
        Staff staff =new Staff();
        staff.setName(name);
        staff.setLoginName(loginName);
        staff.setPassword(password);
        staff.setPhone(phone);
        staff.setEmail(email);
        if (DictConstant.STAFF_ROLE_SYSTEM_MANAGER.equals(role)){
            staff.setRole(DictConstant.STAFF_ROLE_SYSTEM_MANAGER);
        }else {
            staff.setRole(DictConstant.STAFF_ROLE_COMMON_MANAGER);
        }

        staff.setDeptId(Integer.valueOf(deptId));
        staff.setStatus(StatusConstant.STAFF_STATUS_ENABLED);

        staff.setCreateDate(new Date());
        staff.setCreateStaffId(staff2.getId());

        staffMapper.insertSelective(staff);

    }

    @Override
    public void updateStaff(String id, String name, String phone, String email, String role, String deptId) throws RequestParameterErrorException {
        if(ParameterUtil.isNull(name)||ParameterUtil.isNull(phone)||ParameterUtil.isNull(email)||ParameterUtil.isNull(role)||ParameterUtil.isNull(deptId)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        if (!ParameterUtil.setPhone(phone)){
            throw new RequestParameterErrorException("电话号码有误:格式为1开头，总共11位数字");
        }
        if (!ParameterUtil.setEmail(email)){
            throw new RequestParameterErrorException("邮箱有误:格式为XXX@XXX.XXX，X只能是数字或字母");
        }
        Staff staff= staffMapper.selectByPrimaryKey(Integer.valueOf(id));

        staff.setName(name);
        staff.setPhone(phone);
        staff.setEmail(email);
        if (DictConstant.STAFF_ROLE_SYSTEM_MANAGER.equals(role)){
            staff.setRole(DictConstant.STAFF_ROLE_SYSTEM_MANAGER);
        }else {
            staff.setRole(DictConstant.STAFF_ROLE_COMMON_MANAGER);
        }
        staff.setDeptId(Integer.valueOf(deptId));

        staffMapper.updateByPrimaryKeySelective(staff);
    }

    @Override
    public void enableStaff(String id) throws RequestParameterErrorException, DeptStatusErrorException {
        if (ParameterUtil.isNull(id)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        Staff staff = findStaffById(id);
        if(staff.getDeptId()!=null){
            Dept Dept =deptMapper.selectByPrimaryKey(staff.getDeptId());
            if (Dept.getStatus()==StatusConstant.DEPT_STATUS_DISABLED){
                throw new DeptStatusErrorException("请先启用上级部门");
            }else {
                staff.setStatus(StatusConstant.DEPT_STATUS_ENABLED);
            }
        }else {
            staff.setStatus(StatusConstant.DEPT_STATUS_ENABLED);
        }
        staffMapper.updateByPrimaryKeySelective(staff);
    }

    @Override
    public void disableStaff(String id) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        Staff staff = findStaffById(id);
        staff.setStatus(StatusConstant.DEPT_STATUS_DISABLED);
        staffMapper.updateByPrimaryKeySelective(staff);
    }



}
